<template>
  <div>
    <div class="title">{{$t('recipe.data_entry')}}
      <el-button type="primary"
        :disabled="stepObj.phaseDesc != '数据录入' || stepObj.isActive != 1 || phaseIsdis || isStep || isoperation == 1 || unitStat != 10"
        size="small" :loading="sumibtLoading" style="float:right" @click="handleSumibt()">{{$t('run.submit')}}</el-button>
    </div>
    <div class="tableAuto tableAutos">
      <el-table border :data="tableData" v-loading="tableLoading" style="width: 100%" :header-cell-style="{background:'#EAEBEC'}">
        <el-table-column prop="paraName" :label="$t('run.data_name')" align="center" width="100px"></el-table-column>
        <el-table-column prop="hi" :label="$t('common.upper_limit')" align="center" width="100px"></el-table-column>
        <el-table-column prop="lo" :label="$t('common.lower_limit')" align="center" width="100px"></el-table-column>
        <el-table-column prop="data" :label="$t('recipe.data_entry')" align="center" width="100px">
          <template slot-scope="scope" v-if="scope.row.fid">
            <template v-if="scope.row.type != 3">
              <el-input v-if="scope.row.type == 1" type="number" v-enterNumber size="small" v-model="scope.row.target"
                :disabled="scope.row.groups == '3' || (scope.row.groups == null && scope.row.paraName == '启动延时')"></el-input>
              <el-input v-else type="text" size="small" v-model="scope.row.target"
                :disabled="scope.row.groups == '3' || (scope.row.groups == null && scope.row.paraName == '启动延时')"></el-input>
            </template>
            <template v-if="scope.row.type == 3">
              <el-select v-model="scope.row.target" placeholder="" size="small" class="widthBfb">
                <el-option label="true" :value="1"></el-option>
                <el-option label="false" :value="0"></el-option>
              </el-select>
            </template>
          </template>
        </el-table-column>
        <el-table-column prop="groups" :label="$t('run.data_group')" align="center" width="120px">
          <template slot-scope="scope" v-if="scope.row.fid">
            {{getGroupsType(scope.row.groups)}}
          </template>
        </el-table-column>
        <el-table-column prop="type" :label="$t('run.data_type')" align="center" width="100px">
          <template slot-scope="scope" v-if="scope.row.fid">
            {{getDataType(scope.row.type)}}
          </template>
        </el-table-column>
        <el-table-column prop="uom" :label="$t('basic.company')" align="center" width="100px"></el-table-column>
        <el-table-column label="" class="caozuo" align="center">
          <template slot-scope="scope" v-if="scope.row.fid && scope.row.paraName != '启动延时'">
            <el-button type="text" v-if="scope.row.useAttribute == 1 && scope.row.type != 3" :loading="loading" size="small"
              @click="handleConfirm(scope.row)">获取</el-button>
            <!-- :disabled="stepObj.isAuto != 1 || phaseIsdis || isStep || isoperation == 1" -->
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog v-dialogDrag title="签名" :visible.sync="dialogVisible2" width="40%" :before-close="handleCloseSignName"
      :close-on-click-modal="false">
      <el-form :model="loginForm" label-width="85px" ref="loginForm" :rules="loginRules" size="small" :hide-required-asterisk="true">
        <el-row :gutter="24">
          <el-col :xs="24">
            <el-form-item label="用户名" prop="userName">
              <el-input type="text" v-model="loginForm.userName" placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24">
            <el-form-item label="密码" prop="passWord" class="password">
              <el-input type="text" v-model="loginForm.passWord" placeholder=""></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="handleConfirmLogin('loginForm')">确 认</el-button>
        <el-button @click="handleCloseSignName()" size="small">{{ $t("common.cancel") }}</el-button>
      </span>
    </el-dialog>
    <div class="btnfang" style="text-align:center;margin-top:18px">
      <!-- phaseCode  TEM  温度控制暂时把暂停 启动 退出 禁止掉 -->
      <el-button :disabled="(stepObj.isActive != 1 || button1 || phaseIsdis) || isStep || isoperation == 1" type="primary" size="small"
        @click="handleEditPhaseStatus(10,'启动')">{{ $t("run.start_up") }}</el-button>
      <el-button :disabled="stepObj.isActive != 1 || button2 || phaseIsdis || isStep || isoperation == 1 || stepObj.phaseCode == 'PH_TEM'"
        size="small" @click="handleEditPhaseStatus(41,'停止')">{{ $t("run.stop") }}</el-button>
    </div>
  </div>
</template>

<script>
import { plc } from "@/api/plc"
export default {
  props: {
    stepObj: Object,
    count: Number,
    phaseIsdis: Boolean,
    formsSeqNo: [String, Number],
    idObj: Object,
    isoperation: [Number, Object],
    unitStat: Number,
    HandShake: Number
  },
  data () {
    return {
      tableData: ['', '', ''],
      tableLoading: false,
      loading: false,
      sumibtLoading: false,
      dialogVisible2: false,
      loginForm: {
        userName: "",
        passWord: ""
      },
      loginRules: {
        userName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        passWord: [{ required: true, message: '请输入密码', trigger: 'blur' }],
      },
      doneByUserId: '',
      list: [{
        label: "bool",
        value: "1"
      }, {
        label: "byte",
        value: "2"
      }, {
        label: "char",
        value: "3"
      }, {
        label: "int(int32)",
        value: "4"
      }, {
        label: "int16(无符号)",
        value: "5"
      }, {
        label: "float",
        value: "6"
      }, {
        label: "double",
        value: "7"
      }, {
        label: "string",
        value: "8"
      }, {
        label: "int32(无符号)",
        value: "9"
      }]
    }
  },
  watch: {
    count (val) {
      if (this.stepObj.serialNo == '4' && this.stepObj.fid) { //数据录入
        this.findHmiCommon()
      } else {
        this.tableData = ['', '', '']
      }
      // console.log(this.stepObj)
    }
  },
  computed: {
    isStep () {
      if (parseInt(this.stepObj.seqNo || 0) == this.formsSeqNo) {
        return false
      } else {
        return true
      }
    },
    // 6温度控制   9蒸馏-氮气置换  10 蒸馏-冷凝接收
    button1 () { //启动按钮
      let arr = ['1', '2', '3', '5', '11', '12']
      let arr1 = ['0', '7', '8', '4', '6', '9', '10']
      if (this.unitStat === 0 || this.unitStat == 11) {
        if (arr.indexOf(this.stepObj.serialNo) > -1) {
          return false
        } else if (arr1.indexOf(this.stepObj.serialNo) > -1) {
          return false
        } else {
          return true
        }
      } else if (this.unitStat == 51) {
        let arr2 = ['5', '1', '2', '3', '11', '12']
        if (arr2.indexOf(this.stepObj.serialNo) > -1) {
          return false
        } else {
          return true
        }
      } else {
        return true
      }
    },
    button2 () { //停止按钮
      let arr = ['5', '1', '2', '3', '11', '12']
      if (this.unitStat == 10) {
        if (arr.indexOf(this.stepObj.serialNo) > -1) {
          return false
        } else {
          return true
        }
      } else {
        return true
      }
    }
  },
  created () {

  },
  methods: {
    findHmiCommon () {
      const param = {
        phaseId: this.stepObj.fid,
        woId: this.idObj.woId
      }
      this.tableLoading = true
      plc.findHmiCommon(param).then(res => {
        this.tableLoading = false
        if (res.code == 200) {
          if (res.data.length > 0) {
            this.tableData = res.data
            if (this.tableData.length == 0) {
              this.tableData = ['', '', '']
            }
          } else {
            this.tableData = ['', '', '']
          }

        } else {
          this.tableData = ['', '', '']
        }
      })
    },
    handleConfirm (row) {
      if (row.useAttribute == 1) {
        this.$set(row, 'target', this.HandShake)
      }
    },
    handleSumibt () {
      if (this.stepObj.signitature == 1) {
        this.dialogVisible2 = true
        return
      }
      this.doneByUserId = ""
      this.submitDataEntryCommand()
    },
    submitDataEntryCommand () {
      let tableData = this.tableData.map(item => {
        if (item.paraName != '启动延时') {
          return {
            woId: this.idObj.woId,
            // unitId:this.idObj.opId,
            unitId: this.idObj.equipmentId,
            phaseId: item.phaseId,
            paraName: item.paraName,
            type: item.type,
            groups: item.groups,
            uom: item.uom,
            actual: item.target,
            hi: item.hi,
            lo: item.lo,
            spare1: item.spare1,
            spare2: item.spare2,
            spare3: item.spare3,
            spare4: item.spare4,
            seq: item.seq,
            record: item.groups == 3 ? 0 : 3,
            seqNo: this.isEmpty(item.seqNo) ? item.seq : item.seqNo,
            jobId: this.idObj.jobId,
            doneByUserId: this.doneByUserId
          }
        }

      })
      tableData = tableData.filter(item => item)
      this.sumibtLoading = true
      plc.submitDataEntryCommand(tableData).then(res => {
        this.sumibtLoading = false

        if (res.code == 200) {
          this.$message({ duration: 1000, type: 'success', message: '提交成功!' });
        } else {
          this.$message.error(res.msg);
        }
      })
    },
    handleConfirmLogin (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const param = {
            fpsw: this.loginForm.passWord,
            faccount: this.loginForm.userName
          }
          plc.checkSysUser(param).then(res => {
            if (res.code == 200) {
              this.doneByUserId = res.data[0].fid
              this.dialogVisible2 = false
              this.submitDataEntryCommand()
            } else {
              this.$message.error(res.msg);
            }
          })
        }
      })
    },
    handleCloseSignName () {
      this.dialogVisible2 = false
    },
    isEmpty (val) {
      if (val === '' || val === null || val === undefined) {
        return true
      } else {
        return false
      }
    },
    getDataType (id) {
      id = parseFloat(id)
      switch (id) {
        case 0:
          return "Float"
        case 1:
          return "Int"
        case 2:
          return "String"
        case 3:
          return "Bool"
      }
    },
    getGroupsType (type) {
      // {{scope.row.groups == 0 ? '质量数据' : scope.row.groups == 1 ? '过程数据' : ''}}
      type = parseFloat(type)
      switch (type) {
        case 0:
          return "质量数据"
        case 1:
          return "过程数据"
        case 2:
          return ""
        case 3:
          return ""
      }
    },
    isNumber (val) {
      let regPos = /^\d+(\.\d+)?$/; //非负浮点数
      let regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
      if (regPos.test(val) || regNeg.test(val)) {
        return true;
      } else {
        return false;
      }
    },
    handleEditPhaseStatus (type, txt) {
      if (!this.stepObj.fid) {
        this.$message.error('请选择阶段');
        return
      }
      let that = this
      this.$customConfirm({
        content: '是否要' + txt + '此阶段',
        onok () {
          const param = {
            fid: that.stepObj.fid,
            stateCd: type
          }
          plc.sendPlcCommandPhase(param).then(res => {
            if (res.code == 200) {
              that.$message({ duration: 1000, type: 'success', message: txt + '成功!' });
              if (type == 40 && that.stepObj.serialNo == 7) {
                that.submitDescribeCommand()
              }

            }
          })
        },
      })
    },
  }
}
</script>

<style scoped>
.caozuo >>> .el-button {
  padding: 0 20px;
}
.title {
  margin-bottom: 15px;
}
.tableAuto >>> .cell {
  min-height: 23px;
}
.widthBfb {
  width: 100%;
}
.password >>> .el-input {
  -webkit-text-security: disc;
}
</style>
